package c6132.Day3;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Map;
import java.util.Properties;

public class JUtil {
    //1,注册驱动-Driver类，只注册一次
    static String url;
    static String user;
    static String psd;
    static{
    Properties p=new Properties();
        try {
            p.load(new FileInputStream("D:\\Java\\src\\c6132\\Day3\\jdbc.properties"));

           String driverName=  p.getProperty("drivername");
           Class.forName(driverName);
           url=p.getProperty("url");
           user=p.getProperty("user");
           psd=p.getProperty("psd");
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }}

        //建立连接
    public static Connection getConn() throws SQLException {

        return DriverManager.getConnection(url,user,psd);
    }
    public static void free(Statement st,Connection conn) throws SQLException {
        st.close();
        conn.close();
    }
    public static void free(ResultSet re,Statement st, Connection conn) throws SQLException {
        re.close();
        free(st,conn);
    }
}
/*
public class JUtil {
    //1,注册驱动-Driver类，只注册一次
    static{
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    static String url="jdbc:mysql://127.0.0.1:3308/data";
    static String user="root";
    static String psd="123456";//注意：要把可变的变量放到配置文件中


    //建立连接
    public static Connection getConn() throws SQLException {

        return DriverManager.getConnection(url,user,psd);
    }
    public static void free(Statement st,Connection conn) throws SQLException {
        st.close();
        conn.close();
    }
    public static void free(ResultSet re,Statement st, Connection conn) throws SQLException {
        re.close();
        free(st,conn);
    }
}
 */